{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "# Sicherman Dice\n",
    "\n",
    "> Note: This notebook takes the form of a conversation between two problem solvers, *Ali* and ***Bo***. Also note, for those who are not native speakers of English: \"dice\" is the plural form; \"die\" is the singular.\n",
    "\n",
    "Ali: Huh. <a href=\"http://wordplay.blogs.nytimes.com/2014/06/16/dice-3/\">This</a> is interesting.  You know how in many games, such as craps or Monopoly, you roll two dice and add them up. Only the sum matters, not what number either of the individual dice shows.\n",
    "\n",
    "**Bo: Right.**\n",
    "\n",
    "Ali: And some of those sums, like 7, can be made multiple ways, while 2 and 12 can only be made one way. \n",
    "\n",
    "**Bo: Yeah. 8 can be made 5 ways, so it has a 5/36 probability of occuring.**\n",
    "\n",
    "Ali: The interesting thing is that people have been playing dice games for 7,000 years. But it wasn't until 1977 that <a href=\"http://userpages.monmouth.com/~colonel/\">Colonel George Sicherman</a> asked whether is is possible to have a pair of dice that are not regular dice&mdash;that is, they don't have (1, 2, 3, 4, 5, 6) on the six sides&mdash;but have the same distribution of sums as a regular pair&mdash;so the pair of dice would also have to have 5 ways of making 8, but it could be different ways; maybe 7+1 could be one way. Sicherman assumes that each side has a positive integer.\n",
    "\n",
    "**Bo: And what did he find?**\n",
    "\n",
    "Ali: Wouldn't it be more fun to figure it out for ourselves?\n",
    "\n",
    "**Bo: OK!**\n",
    "\n",
    "Ali: How could we proceed?\n",
    "\n",
    "**Bo: When in doubt, [use brute force](http://quotes.lifehack.org/quote/ken-thompson/when-in-doubt-use-brute-force/): we can write a program to enumerate the possibilities:**\n",
    "\n",
    "\n",
    "- **Generate all dice that could possibly be part of a solution, such as (1, 2, 2, 4, 8, 9).**\n",
    "- **Consider all pairs of these dice, such as ((1, 3, 4, 4, 5, 8), (1, 2, 2, 4, 8, 9))**\n",
    "- **See if we find any pairs that are not the regular pair, but do have the same distribution of sums as the regular pair.**\n",
    "\n",
    "\n",
    "Ali: That's great.  I can code up your description almost verbatim. I'll also keep track of our TO DO list:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "def sicherman():\n",
    "    \"\"\"The set of pairs of 6-sided dice that have the same\n",
    "    distribution of sums as a regular pair of dice.\"\"\"\n",
    "    return {pair for pair in pairs(all_dice())\n",
    "            if pair != regular_pair\n",
    "            and sums(pair) == regular_sums}\n",
    "\n",
    "# TODO: pairs, all_dice, regular_pair, sums, regular_sums"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "**Bo: Looks good to me.**\n",
    "\n",
    "Ali: Now we can tick off the items in the TO DO list. The function `pairs` is first, and it is easy:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "def pairs(collection): \n",
    "    \"Return all pairs (A, B) from collection where A <= B.\"\n",
    "    return [(A, B) for A in collection for B in collection if A <= B]\n",
    "\n",
    "# TODO: all_dice, regular_pair, sums, regular_sums"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "**Bo: That's good. We could have used the library function `itertools.combinations_with_replacement`, but let's just leave it as is. We should test to make sure it works:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pairs(['A', 'B', 'C'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "# TO DO: `sums(pair)`\n",
    "\n",
    "\n",
    "Ali: Now for `sums`: we need some way to represent all the 36 possible sums from a pair of dice.  We want a representation that will be the same for two different pairs if all 36 sums are the same, but where the order or composition of the sums doesn't matter. \n",
    "\n",
    "**Bo: So we want a set of the sums?**\n",
    "\n",
    "Ali: Well, it can't be a set, because we need to know that 8 can be made 5 ways, not just that 8 is a member of the set. The technical term for a collection where order doesn't matter but where you can have repeated elements is a **bag**, or sometimes called a [**multiset**](https://en.wikipedia.org/wiki/Multiset).  Can you think of a representation for that?\n",
    "\n",
    "**Bo: Well the easiest is just a sorted list or tuple&mdash;if we don't want order to matter, sorting takes care of that.  Another choice would be a dictionary of {sum: count} entries, like {2: 1, 3: 2, ... 11: 2, 12: 1}. There is even a library class, `collections.Counter`, that does exactly that.**\n",
    "\n",
    "Ali: How do we choose between the two representations?\n",
    "\n",
    "**Bo: I don't think it matters much.  Since there are only 36 entries, I think the sorted list will be simpler, and probably more efficient.  For 100-sided dice I'd probably go with the Counter.**\n",
    "\n",
    "Ali: OK, here's some code for `Bag` and `sums`, and for regular dice:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "Bag = sorted # Implement a bag as a sorted list\n",
    "\n",
    "def sums(pair):\n",
    "    \"All possible sums of a side from one die plus a side from the other.\"\n",
    "    (A, B) = pair\n",
    "    return Bag(a + b for a in A for b in B)\n",
    "\n",
    "def ints(start, end): \n",
    "    \"A tuple of the integers from start to end, inclusive.\"\n",
    "    return tuple(range(start, end + 1))\n",
    "\n",
    "regular_die  = ints(1, 6)\n",
    "regular_pair = (regular_die, regular_die)\n",
    "regular_sums = sums(regular_pair)\n",
    "\n",
    "# TODO: all_dice"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Ali: Let's check the `regular_sums`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "36"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(regular_sums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 3, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 11, 11, 12]\n"
     ]
    }
   ],
   "source": [
    "print(regular_sums)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Bo: And we can see what that would look like to a `Counter`:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 5, 9: 4, 10: 3, 11: 2, 12: 1})"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import collections\n",
    "\n",
    "collections.Counter(regular_sums)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "**Bo: Looks good! Now only one more thing on our TODO list:**\n",
    "\n",
    "# TO DO: `all_dice()`\n",
    "\n",
    "\n",
    "Ali: `all_dice` should generate all possible dice, where by \"possible\" I mean the dice that could feasibly be part of a pair that is a solution to the Sicherman problem. Do we know how many dice that will be? Is it a large enough number that efficiency will be a problem?\n",
    "\n",
    "**Bo: Let's see.  A die has six sides. If each side can be a number from, say, 1 to 10, that's 10<sup>6</sup> or a million possible dice; a million is a small number for a computer.**\n",
    "\n",
    "Ali: True, a million is a relatively small number for `all_dice()`, but how many `pairs(all_dice())` will there be?\n",
    "\n",
    "**Bo: Ah.  A million squared is a trillion.  That's a large number even for a computer. Just counting to a trillion takes hours in Python; checking a trillion pairs will take days.**\n",
    "\n",
    "Ali: So we need to get rid of most of the dice. What about permutations?\n",
    "\n",
    "**Bo: Good point.  If I have the die (1, 2, 3, 4, 5, 6), then I don't need the 6! = 720 different permutations of this die&mdash; that is, dice like (2, 4, 6, 1, 3, 5).\n",
    "Each die should be a bag (I learned a new word!) of sides.  So we've already eliminated 719/720 = 99.9% of the work.**\n",
    "\n",
    "Ali: One other thing bothers me ... how do you know that the sides can range from 1 to 10?  Are you sure that 11 can't be part of a solution? Or 12?\n",
    "\n",
    "**Bo: Every side on every die must be a positive integer, right?**\n",
    "\n",
    "Ali: Right. No zeroes, no negative numbers, no fractions.\n",
    "\n",
    "**Bo: Then I know for sure that 12 can't be on any die, because when you add 12 to whatever is on the other die, you would get at least 13, and 13 is not allowed in the regular distribution of sums.**\n",
    "\n",
    "Ali: Good. How about 11?\n",
    "\n",
    "**Bo: We can't have a sum that is bigger than 12.  So if one die had an 11, the other would have to have all 1s.  That wouldn't work, because then we'd have six 12s, but we only want one. So no side can have a number bigger than 10.**\n",
    "\n",
    "Ali: What else can we say about the biggest number on a die?\n",
    "\n",
    "**Bo: There's one 12 in the sums.  But there are several ways to make a 12: 6+6 or 7+5 or 8+4, and so on.  So I can't say for sure what the biggest number on any one die will be.  But I can say that whatever the biggest number on a die is, it will be involved in summing to 12, so there can be only one of them, because we only want to make one 12.**\n",
    "\n",
    "Ali: What about the smallest number on a die?\n",
    "\n",
    "**Bo: Well, there's only one 2 allowed in the sums.  The only way to sum to 2 is 1+1: a 1 from each of the dice in the pair.  If a die had no 1s, we wouldn't get a 2; if a die had more than one 1, we would get too many 2s.  So every die has to have exactly one 1.**\n",
    "\n",
    "Ali: Good.  So each die has exactly one 1, and exactly one of whatever the biggest number is, something in the range up to 10.  Here's a picture of the six sides of any one die:\n",
    "\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">&nbsp;1&nbsp;</span> &lt;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-10</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-10</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-10</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-10</span> &lt;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-10</span>\n",
    "\n",
    "Ali: The bag of sides is always listed in  non-decreasing order; the first side, 1, is less than the next, and the last side, whatever it is, is greater than the one before it.\n",
    "\n",
    "**Bo: Wait a minute: you have [2-10] &lt; [2-10].  But 2 is not less than 2, and 10 is not less than 10.  I think it should be [2-9] &lt; [3-10]. So the picture should be like this:**\n",
    "\n",
    "\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">&nbsp;1&nbsp;</span> &lt;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-9</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-9</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-9</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-9</span> &lt;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">3-10</span>\n",
    "\n",
    "Ali: Good! We're making progress in cutting down the range. But it That this bothers me because it says the range for the biggest number is 3 to 10. But if one die has a 3 and the other a 10, that adds to 13. So I'm thinking that it is not possible to have a 10 after all&mdash;because if one die had a 10, then the other would have to have a 2 as the biggest number, and that can't be. Therefore the biggest number is in the range of 3 to 9.  But then the others have to be \n",
    "less, so make them 2 to 8:\n",
    "\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">&nbsp;1&nbsp;</span> &lt;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-8</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-8</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-8</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-8</span> &lt;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">3-9</span>\n",
    "\n",
    "**Bo: I can turn this picture into code, and we can see how many dice there are:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "462"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def all_dice():\n",
    "    \"A list of all feasible 6-sided dice for the Sicherman problem.\"\n",
    "    return [(1, s2, s3, s4, s5, s6)\n",
    "            for s2 in ints(2, 8) \n",
    "            for s3 in ints(s2, 8)\n",
    "            for s4 in ints(s3, 8)\n",
    "            for s5 in ints(s4, 8) \n",
    "            for s6 in ints(s5+1, 9)]\n",
    "\n",
    "len(all_dice())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Ali: Nice! We got down from a million to 462 different dice. I think we're ready to run `sicherman()`.  Any bets on what we'll find out?\n",
    "\n",
    "**Bo: I bet that Sicherman is remembered because he discovered a pair of dice that works. If he just proved the non-existence of a pair, I don't think that would be noteworthy.**\n",
    "\n",
    "Ali: Makes sense.  Here goes:\n",
    "\n",
    "# The Answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{((1, 2, 2, 3, 3, 4), (1, 3, 4, 5, 6, 8))}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sicherman()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "**Bo: Look at that!**\n",
    "\n",
    "Ali: It turns out you can <a href=\"http://www.grand-illusions.com/acatalog/Sicherman_Dice.html\">buy</a> a pair of dice with just these numbers. Here's a table I borrowed from [Wikipedia](https://en.wikipedia.org/wiki/Sicherman_dice) that shows both pairs of dice have the same sums.  \n",
    "\n",
    "<table class=\"wikitable\">\n",
    "<tr>\n",
    "<td align=\"centre\"></td>\n",
    "<td align=\"centre\">2</td>\n",
    "<td align=\"centre\">3</td>\n",
    "<td align=\"centre\">4</td>\n",
    "<td align=\"centre\">5</td>\n",
    "<td align=\"centre\">6</td>\n",
    "<td align=\"centre\">7</td>\n",
    "<td align=\"centre\">8</td>\n",
    "<td align=\"centre\">9</td>\n",
    "<td align=\"centre\">10</td>\n",
    "<td align=\"centre\">11</td>\n",
    "<td align=\"centre\">12</td>\n",
    "</tr>\n",
    "<tr>\n",
    "<td>Regular dice:\n",
    "<br>(1, 2, 3, 4, 5, 6)\n",
    "<br>(1, 2, 3, 4, 5, 6)</td>\n",
    "<td>1+1</td>\n",
    "<td>1+2<br />\n",
    "2+1</td>\n",
    "<td>1+3<br />\n",
    "2+2<br />\n",
    "3+1</td>\n",
    "<td>1+4<br />\n",
    "2+3<br />\n",
    "3+2<br />\n",
    "4+1</td>\n",
    "<td>1+5<br />\n",
    "2+4<br />\n",
    "3+3<br />\n",
    "4+2<br />\n",
    "5+1</td>\n",
    "<td>1+6<br />\n",
    "2+5<br />\n",
    "3+4<br />\n",
    "4+3<br />\n",
    "5+2<br />\n",
    "6+1</td>\n",
    "<td>2+6<br />\n",
    "3+5<br />\n",
    "4+4<br />\n",
    "5+3<br />\n",
    "6+2</td>\n",
    "<td>3+6<br />\n",
    "4+5<br />\n",
    "5+4<br />\n",
    "6+3</td>\n",
    "<td>4+6<br />\n",
    "5+5<br />\n",
    "6+4</td>\n",
    "<td>5+6<br />\n",
    "6+5</td>\n",
    "<td>6+6</td>\n",
    "</tr>\n",
    "<tr>\n",
    "<td>Sicherman dice:\n",
    "<br>(<b><span style=\"color:orange;\">1</span>, <span style=\"color:red;\">2</span>, <span style=\"color:blue;\"><i>2</i></span>, <span style=\"color:red;\">3</span>, <span style=\"color:blue;\"><i>3</i></span>, <span style=\"color:orange;\">4</span></b>)\n",
    "<br>(1, 3, 4, 5, 6, 8)</td>\n",
    "<td><b><span style=\"color:orange;\">1</span></b>+1</td>\n",
    "<td><b><span style=\"color:red;\">2</span></b>+1<br />\n",
    "<b><span style=\"color:blue;\"><i>2</i></span></b>+1</td>\n",
    "<td><b><span style=\"color:red;\">3</span></b>+1<br />\n",
    "<b><span style=\"color:blue;\"><i>3</i></span></b>+1<br />\n",
    "<b><span style=\"color:orange;\">1</span></b>+3</td>\n",
    "<td><b><span style=\"color:orange;\">1</span></b>+4<br />\n",
    "<b><span style=\"color:red;\">2</span></b>+3<br />\n",
    "<b><span style=\"color:blue;\"><i>2</i></span></b>+3<br />\n",
    "<b><span style=\"color:orange;\">4</span></b>+1</td>\n",
    "<td><b><span style=\"color:orange;\">1</span></b>+5<br />\n",
    "<b><span style=\"color:red;\">2</span></b>+4<br />\n",
    "<b><span style=\"color:blue;\"><i>2</i></span></b>+4<br />\n",
    "<b><span style=\"color:red;\">3</span></b>+3<br />\n",
    "<b><span style=\"color:blue;\"><i>3</i></span></b>+3</td>\n",
    "<td><b><span style=\"color:orange;\">1</span></b>+6<br />\n",
    "<b><span style=\"color:red;\">2</span></b>+5<br />\n",
    "<b><span style=\"color:blue;\"><i>2</i></span></b>+5<br />\n",
    "<b><span style=\"color:red;\">3</span></b>+4<br />\n",
    "<b><span style=\"color:blue;\"><i>3</i></span></b>+4<br />\n",
    "<b><span style=\"color:orange;\">4</span></b>+3</td>\n",
    "<td><b><span style=\"color:red;\">2</span></b>+6<br />\n",
    "<b><span style=\"color:blue;\"><i>2</i></span></b>+6<br />\n",
    "<b><span style=\"color:red;\">3</span></b>+5<br />\n",
    "<b><span style=\"color:blue;\"><i>3</i></span></b>+5<br />\n",
    "<b><span style=\"color:orange;\">4</span></b>+4</td>\n",
    "<td><b><span style=\"color:orange;\">1</span></b>+8<br />\n",
    "<b><span style=\"color:red;\">3</span></b>+6<br />\n",
    "<b><span style=\"color:blue;\"><i>3</i></span></b>+6<br />\n",
    "<b><span style=\"color:orange;\">4</span></b>+5</td>\n",
    "<td><b><span style=\"color:red;\">2</span></b>+8<br />\n",
    "<b><span style=\"color:blue;\"><i>2</i></span></b>+8<br />\n",
    "<b><span style=\"color:orange;\">4</span></b>+6<br /></td>\n",
    "<td><b><span style=\"color:red;\">3</span></b>+8<br />\n",
    "<b><span style=\"color:blue;\"><i>3</i></span></b>+8</td>\n",
    "<td><b><span style=\"color:orange;\">4</span></b>+8</td>\n",
    "</tr>\n",
    "</table>\n",
    "\n",
    "\n",
    "Ali: We could stop here. Or we could try to solve it for *N*-sided dice.\n",
    "\n",
    "# Why stop now? Onward!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Ali: OK. I know 4-, 12-, and 20-sided dice are common, but we'll try to handle any *N* > 1. My guess is we won't go too far before our program becomes too slow. So, before we try *N*-sided dice, let's analyze six-sided dice a little better, to see if we can eliminate some of the pairs before we start. The picture says that (1, 2, 2, 2, 2, 3) could be a valid die.  Could it?\n",
    "\n",
    "**Bo: No! If a die had four 2s, then we know that since the other die has one 1, we could make 2 + 1 = 3 four ways. But the `regular_sums` has  only two 3s. So that means that a die can have no more than two 2s.  New picture:**\n",
    "\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">&nbsp;1&nbsp;</span> &lt;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-8</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-8</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">3-8</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">3-8</span> &lt;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">3-9</span>\n",
    "\n",
    "Ali: Now we've got [3-8] < [3-9]; that's not right.  If a die can only have one 1 and two 2s, then it must have at least one number that is a 3 or more, followed by the biggest number, which must be 4 or more, and we know a pair of biggest numbers must sum to 12, so the range of the biggest can't be [4-9], it must be [4-8]:\n",
    "\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">&nbsp;1&nbsp;</span> &lt;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-7</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-7</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">3-7</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">3-7</span> &lt;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">4-8</span>\n",
    "\n",
    "We can update the code and count again:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "231"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def all_dice():\n",
    "    \"A list of all feasible 6-sided dice for the Sicherman problem.\"\n",
    "    return [(1, s2, s3, s4, s5, s6)\n",
    "            for s2 in ints(2, 7) \n",
    "            for s3 in ints(s2, 7)\n",
    "            for s4 in ints(max(s3, 3), 7)\n",
    "            for s5 in ints(s4, 7) \n",
    "            for s6 in ints(s5+1, 8)]\n",
    "\n",
    "len(all_dice())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "**Bo: Nice&mdash;we cut the number of dice in half.  I don't want to print `all_dice()`, but I can sample a few:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 2, 3, 3, 5, 7),\n",
       " (1, 2, 2, 5, 6, 7),\n",
       " (1, 4, 4, 5, 7, 8),\n",
       " (1, 3, 3, 6, 6, 8),\n",
       " (1, 4, 4, 4, 4, 5),\n",
       " (1, 4, 5, 5, 7, 8),\n",
       " (1, 2, 3, 6, 6, 8),\n",
       " (1, 2, 2, 3, 6, 8),\n",
       " (1, 3, 4, 5, 7, 8),\n",
       " (1, 6, 6, 6, 6, 7)]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "random.sample(all_dice(), 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "# `sicherman(N)`\n",
    "\n",
    "Ali: OK, I think we're ready to update `sicherman()`  to `sicherman(N)`. \n",
    "\n",
    "**Bo: Sure, most of that will be easy, just parameterizing with `N`:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "def sicherman(N=6):\n",
    "    \"\"\"The set of pairs of N-sided dice that have the same\n",
    "    distribution of sums as a regular pair of N-sided dice.\"\"\"\n",
    "    reg_sums = regular_sums(N)\n",
    "    reg_pair = regular_pair(N)\n",
    "    return {pair for pair in pairs(all_dice(N))\n",
    "            if pair != reg_pair\n",
    "            and sums(pair) == reg_sums}\n",
    "\n",
    "def regular_die(N):  return ints(1, N)\n",
    "def regular_pair(N): return (regular_die(N), regular_die(N))\n",
    "def regular_sums(N): return sums(regular_pair(N))\n",
    "\n",
    "# TODO: all_dice(N)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Ali: Good. I think it would be helpful  to look at a table of `regular_sums`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "N: 1 {2: 1}\n",
      "N: 2 {2: 1, 3: 2, 4: 1}\n",
      "N: 3 {2: 1, 3: 2, 4: 3, 5: 2, 6: 1}\n",
      "N: 4 {2: 1, 3: 2, 4: 3, 5: 4, 6: 3, 7: 2, 8: 1}\n",
      "N: 5 {2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 4, 8: 3, 9: 2, 10: 1}\n",
      "N: 6 {2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 5, 9: 4, 10: 3, 11: 2, 12: 1}\n",
      "N: 7 {2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 7, 9: 6, 10: 5, 11: 4, 12: 3, 13: 2, 14: 1}\n"
     ]
    }
   ],
   "source": [
    "for N in ints(1, 7):\n",
    "    print(\"N:\", N, dict(collections.Counter(regular_sums(N))))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "**Bo: That is helpful. I can see that any `regular_sums` must have one 2 and two 3s, and three 4s, and so on, not just for `N=6` but for any `N` (except for trivially small `N`). And that means that any regular die can have at most two 2s, three 3s, four 4s, and so on. So we have this picture:**\n",
    "\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">&nbsp;1&nbsp;</span> &lt;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2+</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2+</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">3+</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">3+</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">3+</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">4+</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">4+</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">4+</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">4+</span> &le; ...\n",
    "\n",
    "**where [2+] means the lower bound is 2, but we haven't figured out yet what the upper bound is.**\n",
    "\n",
    "Ali: Let's figure out upper bounds starting from the biggest number. What can the biggest number be?\n",
    "\n",
    "**Bo: For a pair of *N*-sided die, the biggest sides from each one must add up to 2*N*. Let's take *N*=10 as an example. The biggest numbers on two 10-sided Sicherman dice must sum to 20. According to the picture above, the lower bound on the biggest number would be 4, but because there can only be one of the biggest number, the lower bound is 5. So to add up to 20, the range must be [5-15]:**\n",
    "\n",
    "\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">&nbsp;1&nbsp;</span> &lt;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2+</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2+</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">3+</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">3+</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">3+</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">4+</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">4+</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">4+</span> &lt;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">5-15</span> \n",
    "\n",
    "Ali: There's probably some tricky argument for the upper bounds of the other sides, but I'm just going to say the upper bound is one less than the upper bound of the biggest number:\n",
    "\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">&nbsp;1&nbsp;</span> &lt;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-14</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">2-14</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">3-14</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">3-14</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">3-14</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">4-14</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">4-14</span> &le;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">4-14</span> &lt;\n",
    "<span style=\"border-style: solid; width: 5em; height: 5em; padding: 3px\">5-15</span> \n",
    "\n",
    "\n",
    "Let's start by coding up  `lower_bounds(N)`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "button": false,
    "collapsed": true,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "def lower_bounds(N):\n",
    "    \"A list of lower bounds for respective sides of an N-sided die.\"\n",
    "    lowers = [1]\n",
    "    for _ in range(N-1):\n",
    "        m = lowers[-1] # The last number in lowers so far\n",
    "        lowers.append(m if (lowers.count(m) < m) else m + 1)\n",
    "    lowers[-1] = lowers[-2] + 1\n",
    "    return lowers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 2, 3, 3, 4]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lower_bounds(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 2, 3, 3, 3, 4, 4, 4, 5]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lower_bounds(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Ali: And `upper_bounds(N)`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "button": false,
    "collapsed": true,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "def upper_bounds(N):\n",
    "    \"A list of upper bounds for respective sides of an N-sided die.\"\n",
    "    U = 2 * N - lower_bounds(N)[-1]\n",
    "    return [1] + (N - 2) * [U - 1] + [U]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 7, 7, 7, 7, 8]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "upper_bounds(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 14, 14, 14, 14, 14, 14, 14, 14, 15]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "upper_bounds(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Ali: Now, what do we have to do for `all_dice(N)`?  When we knew we had six sides, we wrote six nested loops. We can't do that for *N* sides, so what do we do?\n",
    "\n",
    "**Bo: Here's an iterative approach: we keep track of a list of partially-formed dice, and on each iteration, we add a side to all the partially-formed dice in all possible ways, until the dice all have `N` sides. So for eaxmple, we'd start with:**\n",
    "\n",
    "    dice = [(1,)]\n",
    "    \n",
    "**and then on the next iteration (let's assume *N*=6, so the lower bound is 2 and the upper bound is 7), we'd get this:**\n",
    "\n",
    "    dice = [(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7)]\n",
    "    \n",
    "**on the next iteration, we find all the ways of adding a third side, and so on. Like this:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "def all_dice(N):\n",
    "    \"Return a list of all possible N-sided dice for the Sicherman problem.\"\n",
    "    lowers = lower_bounds(N)\n",
    "    uppers = upper_bounds(N)\n",
    "    def possibles(die, i):\n",
    "        \"The possible numbers for the ith side of an N-sided die.\"\n",
    "        return ints(max(lowers[i], die[-1] + int(i == N-1)),\n",
    "                    uppers[i])\n",
    "    dice = [(1,)]\n",
    "    for i in range(1, N):\n",
    "        dice = [die + (side,)\n",
    "                for die in dice\n",
    "                for side in possibles(die, i)]\n",
    "    return dice"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "**Bo: The tricky part was with the `max`: the actual lower bound at least `lowers[i]`,  but it must be as big as the previous side, `die[-1]`. And just to make things complicated, the very last side has to be strictly bigger than the previous;  `\" + int(i == N-1)\"` does that by adding 1 just in case we're on the last side, and 0 otherwise.**\n",
    "\n",
    "**Let's check it out:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "231"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(all_dice(6))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Ali: Reassuring that we get the same number we got with the old version of `all_dice()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 4, 5, 5, 7, 8),\n",
       " (1, 4, 4, 5, 6, 8),\n",
       " (1, 2, 6, 6, 6, 7),\n",
       " (1, 3, 6, 6, 7, 8),\n",
       " (1, 2, 2, 3, 6, 7),\n",
       " (1, 2, 2, 3, 6, 8),\n",
       " (1, 3, 3, 3, 3, 6),\n",
       " (1, 3, 4, 5, 5, 7)]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.sample(all_dice(6), 8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "# Running `sicherman(N)` for small `N`\n",
    "\n",
    "Ali: Let's try `sicherman(N)` for some small values of `N`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2: set(),\n",
       " 3: set(),\n",
       " 4: {((1, 2, 2, 3), (1, 3, 3, 5))},\n",
       " 5: set(),\n",
       " 6: {((1, 2, 2, 3, 3, 4), (1, 3, 4, 5, 6, 8))}}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{N: sicherman(N)\n",
    " for N in ints(2, 6)}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Again, reassuring that we get the same result for `sicherman(6)`.  And interesting that there is a result for `sicherman(4)` but not for the other *N*.\n",
    "\n",
    "Let's go onwards from *N*=6, but let's check the timing as we go:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 277 ms, sys: 9.04 ms, total: 286 ms\n",
      "Wall time: 344 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{((1, 2, 2, 3, 3, 4), (1, 3, 4, 5, 6, 8))}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%time sicherman(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 14.4 s, sys: 303 ms, total: 14.7 s\n",
      "Wall time: 16.1 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%time sicherman(7)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "# Estimating run time of  `sicherman(N)` for larger `N`\n",
    "\n",
    "Ali: OK, it takes 50 times longer to do 7, compared to 6. At that rate, *N*=8 will take 15 minutes, 9 will take 15 hours, and 10 will take a month.\n",
    "\n",
    "**Bo: Do we know it will continue to rise at the same rate? You're saying the run time is exponential in *N*? **\n",
    "\n",
    "Ali: I think so. The run time is proportional to the number of pairs.  The number of pairs is proportional to the square of the number of dice. The number of dice is roughly exponential in *N*, because each time you increase *N* by 1, you have to try a number of new sides that is similar to the number for the previous side (but not quite the same). I should plot the number of pairs on a log scale and see if it looks like a straight line.\n",
    "\n",
    "I can count the number of pairs without explicitly generating the pairs. If there are *D* dice, then the number of pairs is what? Something like *D* &times; (*D* + 1) / 2?  Or is it *D* &times; (*D* - 1) / 2?\n",
    "\n",
    "**Bo: Let's draw a picture. With *D* = 4, here are all the ways to pair one die with another to yield 10 distinct pairs:**\n",
    "    \n",
    "    11 .. .. ..\n",
    "    21 22 .. ..\n",
    "    31 32 33 ..\n",
    "    41 42 43 44\n",
    "    \n",
    "**To figure out the formula, add a row to the top:**\n",
    "\n",
    "    .. .. .. ..\n",
    "    11 .. .. ..\n",
    "    21 22 .. ..\n",
    "    31 32 33 ..\n",
    "    41 42 43 44\n",
    "    \n",
    "**Now we have a *D* &times; (*D* + 1) rectangle, and we can see that half (10) of them are pairs, and half (the other 10) are not pairs (because they would be repetitions).  So the formula is *D* &times; (*D* + 1)/2, and checking for *D*=4, (4 &times; 5) / 2 = 10, so we're good.**\n",
    "    \n",
    "Ali: OK, let's try it. First some boilerplate for plotting:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import matplotlib\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def logplot(X, Y, *options):\n",
    "    \"Plot Y on a log scale vs X.\"\n",
    "    fig, ax = plt.subplots()\n",
    "    ax.set_yscale('log')\n",
    "    ax.plot(X, Y, *options)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Now we can plot and display the number of pairs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2: 1,\n",
       " 3: 1,\n",
       " 4: 55,\n",
       " 5: 496,\n",
       " 6: 26796,\n",
       " 7: 1274406,\n",
       " 8: 17502486,\n",
       " 9: 823794345,\n",
       " 10: 37613250675,\n",
       " 11: 1688821500366,\n",
       " 12: 25925418283128}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAD8CAYAAABw1c+bAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAHWxJREFUeJzt3Xl0leW59/HvBQgaZ5E6gCRYkRJbWzTiPNcKiuBUS4yW\nKkukFhWPExzO0XpceChYlxMLCIJTA4i8eF5EnI68Fj0MEhwQxIEihCCaIFZRZAhc7x93OMSQSIa9\n97P3fn6ftVzhufcOz7XXwl/u3M89mLsjIiLx0SLqAkREJLUU/CIiMaPgFxGJGQW/iEjMKPhFRGJG\nwS8iEjMKfhGRmFHwi4jEjIJfRCRmWkVdQF0OPvhgz8vLi7oMEZGMsmjRonXu3m5370vL4M/Ly6O0\ntDTqMkREMoqZrWrI+zTUIyISMwp+EZGYUfCLiMSMgl9EJGYU/CIiMaPgFxFJAyUlkJcHLVqEryUl\nybtXWk7nFBGJk5ISGDAANm4M16tWhWuAoqLE3089fhGRiA0btjP0d9i4MbQng4JfRCRiZWWNa28u\nBb+ISETWroVrrgH3ul/v2DE591Xwi4ik2KZNcN990LkzTJoEvXrBXnv98D05OTB8eHLun/DgN7Mj\nzWyCmU2r0dbVzMaa2TQz+2Oi7ykikgncYdo06No1jN+fdx588AE8/zyMHw+5uWAWvhYXJ+fBLjQw\n+M1soplVmNmSWu09zOwjM1tuZkMA3H2Fu/ev+T53X+buA4ErgFMTVbyISKZ49104+2z47W9h333h\ntdfguefgpz8NrxcVwcqVsH17+Jqs0IeG9/ifAHrUbDCzlsBooCeQDxSaWX59f4GZ9QZeAGY1qVIR\nkQxUURGmZh53HCxdCmPGwNtvwznnRFdTg4Lf3ecA62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IeMndfwb8kiz/7GbWHrgJKHD3nwMtgb7RVpUUTwA9\narUNAV5z987Aa9XX2eQJdv3MrwI/d/djgY+BoYm+adYEv7uvdfe3q/+8gRAG7aOtKvnMrANwIfBY\n1LWkgpntD5wBTABw9y3u/s9oq0qJVsBeZtYKyAE+i7iehHP3OcD6Ws19gCer//wkcHFKi0qyuj6z\nu7/i7jtO8p4PdEj0fbMm+GsyszygG7Ag2kpS4kHgDmB71IWkSCegEni8enjrMTPbO+qiksnd1wD3\nA2XAWuBrd38l2qpS5hB3X1v958+BQ6IsJgLXAi8m+i/NuuA3s32A/wMMdvdvoq4nmcysF1Dh7oui\nriWFWgHHAWPcvRvwHdn36/8PVI9r9yH80Dsc2NvMroq2qtTzMAUxNtMQzWwYYQi7JNF/d1YFv5nt\nQQj9EnefHnU9KXAq0NvMVgJTgHPM7G/RlpR05UC5u+/4bW4a4QdBNvs18Km7V7r7VmA6cErENaXK\nF2Z2GED114qI60kJM/sD0Aso8iTMuc+a4DczI4z7LnP3B6KuJxXcfai7d3D3PMLDvtnuntU9QXf/\nHFhtZl2qm84FPoiwpFQoA04ys5zqf+fnkuUPtGuYAfSr/nM/4P9GWEtKmFkPwvBtb3ffmIx7ZE3w\nE3q/VxN6ve9W/3dB1EVJUtwIlJjZYuBXwH0R15NU1b/dTAPeBt4n/H+bdStazWwyMA/oYmblZtYf\nGAGcZ2afEH7zGRFljYlWz2d+FNgXeLU6x8Ym/L5auSsiEi/Z1OMXEZEGUPCLiMSMgl9EJGYU/CIi\nMaPgFxGJGQW/iEjMKPhFRGJGwS8iEjP/H9rWqoaibNdXAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x115b7b0b8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def plot_pairs(Ns):\n",
    "    \"Given a list of N values, plot the number of pairs and return a dict of them.\"\n",
    "    Ds = [len(all_dice(N)) for N in Ns]\n",
    "    Npairs = [D * (D + 1) // 2 for D in Ds]\n",
    "    logplot(Ns, Npairs, 'bo-')\n",
    "    return {Ns[i]: Npairs[i] for i in range(len(Ns))}\n",
    "\n",
    "plot_pairs(ints(2, 12))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Ali: OK, we've learned two things. One, it *is* roughly a straight line on a log scale, so the number of pairs is roughly exponential. Two, there are a *lot* of pairs. 10<sup>14</sup>, just for *N*=12. I don't want to even think about *N*=20.\n",
    "\n",
    "**Bo: So if we want to get much beyond *N*=8, we're either going to need a brand new approach, or we need to make far fewer pairs of dice.**\n",
    "\n",
    "\n",
    "\n",
    "# Making Fewer `pairs`\n",
    "\n",
    "Ali: Maybe we could tighten up the upper bounds on dice, but I don't think that will help very much.\n",
    "How about if we concentrate on making fewer pairs, without worrying about making fewer dice?\n",
    "\n",
    "**Bo: How could we do that? Isn't the number of pairs always (*D*<sup>2</sup> + *D*)/2 ?**\n",
    "\n",
    "Ali: Remember, we're looking for *feasible* pairs. So if there was some way of knowing ahead of time that two dice were incompatible as a pair, we wouldn't even need to consider the pair.\n",
    "\n",
    "**Bo: By incompatible, you mean they can't form a pair that is a solution.**\n",
    "\n",
    "Ali: Right. Consider this: in any valid pair, the sum of the biggest number on each die must be 2*N*.  For example, with *N* = 6:\n",
    "\n",
    "    ((1, 2, 2, 3, 3, 4), (1, 3, 4, 5, 6, 8))    sum of biggests = 4 + 8 = 12\n",
    "    ((1, 2, 3, 4, 5, 6), (1, 2, 3, 4, 5, 6))    sum of biggests = 6 + 6 = 12\n",
    "    \n",
    "So if we have a die with biggest number 7, what dice should we consider pairing it with?\n",
    "\n",
    "**Bo: Only ones with biggest number 5.**\n",
    "\n",
    "**I get it: we sort all the die into bins labeled with their biggest number.  Then we look at each bin, and for the \"7\" bin, we pair them up with the dice in the \"5\" bin. In general, the *B* bin can only pair with the 2*N* - *B* bin.**\n",
    "\n",
    "Ali: Exactly. \n",
    "\n",
    "**Bo: Cool. I can see how that can cut the amount of work by a factor of 10 or so. But  I was hoping for a factor of a million or so.**\n",
    "\n",
    "Ali: There are other properties of a feasible pair.\n",
    "\n",
    "**Bo: Like what?**\n",
    "\n",
    "Ali: Well, what about the number of 2s in a pair?\n",
    "\n",
    "**Bo: Let's see.  We know that any `regular_sums` has to have two 3s, and the only way to make a 3 is 2+1.  And each die has only one 1, so that means that each pair of dice has to have a total of exactly two 2s.**\n",
    "\n",
    "Ali: Does it have to be one 2 on each die?\n",
    "\n",
    "**No.  It could be one each, or it could be two on one die and none on the other. So a die with *T* twos can only pair with dice that have 2 - *T* twos.**\n",
    "\n",
    "Ali: Great.  Can you think of another property?\n",
    "\n",
    "**Bo: Give me a hint.**\n",
    "\n",
    "Ali: Let's look at the sums of 6-sided Sicherman and regular pairs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "42"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum((1, 2, 2, 3, 3, 4) + (1, 3, 4, 5, 6, 8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "42"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum((1, 2, 3, 4, 5, 6) + (1, 2, 3, 4, 5, 6))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "**Bo: They're the same. Is that [the question](http://hitchhikers.wikia.com/wiki/42) that 42 is the answer to? But does a Sicherman pair always have to have the same sum as a regular pair? I guess it doea, because the sum of `sums(pair)` is just all the sides added up *N* times each, so two pairs have the same sum of `sums(pair)` if and only if they have the same sum.**\n",
    "\n",
    "Ali: So consider the die (1, 3, 3, 3, 4, 5). What do we know about the dice that it can possibly pair with?\n",
    "\n",
    "**Bo: OK, that die has a biggest side of 5, so it can only pair with dice that have a biggest side of 12 - 5 = 7. It has a sum of 19, so it can only pair with dice that have a sum of 42 - 19 = 23. And it has no 2s, so it can only pair with dice that have two 2s.**\n",
    "\n",
    "Ali: I wonder how many such dice there are, out of all 231 `all_dice(6)`?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(1, 2, 2, 5, 6, 7)}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{die for die in all_dice(6) if max(die) == (12 - 5) and sum(die) == (42 - 19) and die.count(2) == 2}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "**Bo: There's only one! So, (1, 3, 3, 3, 4, 5) only has to try to pair with one die, rather than 231. Nice improvement!**\n",
    "\n",
    "Ali: In general, I wonder what the sum of the sides of a regular pair is?\n",
    "\n",
    "**Bo: Easy, that's `N * (N + 1)`. [Gauss](http://betterexplained.com/articles/techniques-for-adding-the-numbers-1-to-100/) knew that when he was in elementary school!**\n",
    "\n",
    "# More efficient `pairs(dice)`\n",
    "\n",
    "**Bo: OK, we can code this up easily enough**:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "from collections import defaultdict\n",
    "\n",
    "def tabulate(dice):\n",
    "    \"\"\"Put all dice into bins in a hash table, keyed by bin_label(die).\n",
    "    Each bin holds a list of dice with that key.\"\"\"\n",
    "    # Example: {(21, 6, 1): [(1, 2, 3, 4, 5, 6), (1, 2, 3, 4, 4, 7), ...]\n",
    "    table = defaultdict(list)\n",
    "    for die in dice:\n",
    "        table[bin_label(die)].append(die)\n",
    "    return table\n",
    "\n",
    "def pairs(dice):\n",
    "    \"Return all pairs of dice that could possibly be a solution to the Sicherman problem.\"\n",
    "    table = tabulate(dice)\n",
    "    N = len(dice[0])\n",
    "    for bin1 in table:\n",
    "        bin2 = compatible_bin(bin1, N)\n",
    "        if bin2 in table and bin1 <= bin2:\n",
    "            for A in table[bin1]:\n",
    "                for B in table[bin2]:\n",
    "                    yield (A, B)\n",
    "\n",
    "def bin_label(die): return sum(die), max(die), die.count(2)\n",
    "\n",
    "def compatible_bin(bin1, N): \n",
    "    \"Return a bin label that is compatible with bin1.\"\n",
    "    (S1, M1, T1) = bin1\n",
    "    return (N * (N + 1) - S1, 2 * N - M1, 2 - T1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "**Bo: Let's make sure we haven't broken anything:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2: set(),\n",
       " 3: set(),\n",
       " 4: {((1, 2, 2, 3), (1, 3, 3, 5))},\n",
       " 5: set(),\n",
       " 6: {((1, 2, 2, 3, 3, 4), (1, 3, 4, 5, 6, 8))}}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{N: sicherman(N)\n",
    " for N in ints(2, 6)}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Ali: Good, those are the same answers as before. But how much faster is it?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 21.3 ms, sys: 2.41 ms, total: 23.7 ms\n",
      "Wall time: 26.4 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%time sicherman(7)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Ali: Wow, that's 1000 times faster than before.\n",
    "\n",
    "**Bo: I want to take a peek at what some of the bins look like:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defaultdict(list,\n",
       "            {(12, 4, 2): [(1, 2, 2, 3, 4)],\n",
       "             (13, 4, 1): [(1, 2, 3, 3, 4)],\n",
       "             (13, 5, 2): [(1, 2, 2, 3, 5)],\n",
       "             (14, 4, 0): [(1, 3, 3, 3, 4)],\n",
       "             (14, 5, 1): [(1, 2, 3, 3, 5)],\n",
       "             (14, 5, 2): [(1, 2, 2, 4, 5)],\n",
       "             (14, 6, 2): [(1, 2, 2, 3, 6)],\n",
       "             (15, 5, 0): [(1, 3, 3, 3, 5)],\n",
       "             (15, 5, 1): [(1, 2, 3, 4, 5)],\n",
       "             (15, 6, 1): [(1, 2, 3, 3, 6)],\n",
       "             (15, 6, 2): [(1, 2, 2, 4, 6)],\n",
       "             (16, 5, 0): [(1, 3, 3, 4, 5)],\n",
       "             (16, 5, 1): [(1, 2, 4, 4, 5)],\n",
       "             (16, 6, 0): [(1, 3, 3, 3, 6)],\n",
       "             (16, 6, 1): [(1, 2, 3, 4, 6)],\n",
       "             (16, 6, 2): [(1, 2, 2, 5, 6)],\n",
       "             (17, 5, 0): [(1, 3, 4, 4, 5)],\n",
       "             (17, 6, 0): [(1, 3, 3, 4, 6)],\n",
       "             (17, 6, 1): [(1, 2, 3, 5, 6), (1, 2, 4, 4, 6)],\n",
       "             (18, 5, 0): [(1, 4, 4, 4, 5)],\n",
       "             (18, 6, 0): [(1, 3, 3, 5, 6), (1, 3, 4, 4, 6)],\n",
       "             (18, 6, 1): [(1, 2, 4, 5, 6)],\n",
       "             (19, 6, 0): [(1, 3, 4, 5, 6), (1, 4, 4, 4, 6)],\n",
       "             (19, 6, 1): [(1, 2, 5, 5, 6)],\n",
       "             (20, 6, 0): [(1, 3, 5, 5, 6), (1, 4, 4, 5, 6)],\n",
       "             (21, 6, 0): [(1, 4, 5, 5, 6)],\n",
       "             (22, 6, 0): [(1, 5, 5, 5, 6)]})"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tabulate(all_dice(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "**Bo: Pretty good: four of the bins have two dice, and all the rest have only one die.**\n",
    "\n",
    "Ali: And let's see how many pairs we're producing now.  We'll tabulate *N* (the number of sides); *D* (the number of *N*-sided dice), the number `pairs(dice)` using the new `pairs`, and the number using the old `pairs`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " N:         D #pairs(dice)         D*(D-1)/2\n",
      " 2:         1            1                 0\n",
      " 3:         1            1                 0\n",
      " 4:        10            3                45\n",
      " 5:        31            9               465\n",
      " 6:       231           71            26,565\n",
      " 7:     1,596          670         1,272,810\n",
      " 8:     5,916        6,614        17,496,570\n",
      " 9:    40,590       76,215       823,753,755\n",
      "10:   274,274      920,518    37,612,976,401\n",
      "11: 1,837,836   11,506,826 1,688,819,662,530\n"
     ]
    }
   ],
   "source": [
    "print(' N:         D #pairs(dice)         D*(D-1)/2')\n",
    "for N in ints(2, 11):\n",
    "    dice = list(all_dice(N))\n",
    "    D = len(dice)\n",
    "    print('{:2}: {:9,d} {:12,d} {:17,d}'.format(N, D, len(list(pairs(dice))), D*(D-1)//2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "collapsed": true,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Ali: OK, we're doing 100,000 times better for *N*=11. But it would still take a long time to test 11 million pairs. Let's just get the answers up to *N*=10:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "button": false,
    "collapsed": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 21.4 s, sys: 197 ms, total: 21.6 s\n",
      "Wall time: 22 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{2: set(),\n",
       " 3: set(),\n",
       " 4: {((1, 2, 2, 3), (1, 3, 3, 5))},\n",
       " 5: set(),\n",
       " 6: {((1, 2, 2, 3, 3, 4), (1, 3, 4, 5, 6, 8))},\n",
       " 7: set(),\n",
       " 8: {((1, 2, 2, 3, 3, 4, 4, 5), (1, 3, 5, 5, 7, 7, 9, 11)),\n",
       "  ((1, 2, 2, 3, 5, 6, 6, 7), (1, 3, 3, 5, 5, 7, 7, 9)),\n",
       "  ((1, 2, 3, 3, 4, 4, 5, 6), (1, 2, 5, 5, 6, 6, 9, 10))},\n",
       " 9: {((1, 2, 2, 3, 3, 3, 4, 4, 5), (1, 4, 4, 7, 7, 7, 10, 10, 13))},\n",
       " 10: {((1, 2, 2, 3, 3, 4, 4, 5, 5, 6), (1, 3, 5, 6, 7, 8, 9, 10, 12, 14))}}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "{N: sicherman(N)\n",
    " for N in ints(2, 10)}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "deletable": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Ali: Not bad; we solved it for all *N* up to 10 in just a few seconds more than it took for just *N*=7 with the old version. Interestingly, there are solutions for all composite *N* but no prime *N*. So I have some questions:\n",
    "\n",
    "- Do all composite *N* have solutions?\n",
    "- Do no prime *N* have solutions?\n",
    "- Do some *N* have more than the 3 solutions that 8 has?\n",
    "- Does the fact that 8 has 3 solutions have anything to do with the fact that 8 is a perfect cube?\n",
    "- Could we handle larger *N* if we worked with [generating functions](https://en.wikipedia.org/wiki/Sicherman_dice)?\n",
    "\n",
    "\n",
    "**Bo: Good questions, but I think I'd rather leave those for the reader to deal with (hi, reader!).**\n",
    "\n",
    "**I do have one other question I'd like to handle, though: are there Sicherman *triples*? **\n",
    "\n",
    "# Sicherman Triples\n",
    "\n",
    "Ali: You mean three dice that add up to the same sums as a \"regular\" set of three dice?\n",
    "\n",
    "**Bo: Right. I can define `triples(dice)` in analogy to `pairs(dice)`. For now, I'm only interested in *N*=6, so I won't bother making an efficient version that tabulates into bins. I'll also extend `sums` to handle triples:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import itertools\n",
    "\n",
    "def triples(dice): \n",
    "    \"Return all triples (A, B, C) from dice where A <= B <= C and sum matches regular.\"\n",
    "    regular_sum3 = 3 * sum(regular_die(6))\n",
    "    return [(A, B, C) for A in dice for B in dice for C in dice \n",
    "            if A <= B <= C and sum(A) + sum(B) + sum(C) == regular_sum3]\n",
    "\n",
    "def sums(dice):\n",
    "    \"All possible sums of a side from each of the dice.\"\n",
    "    return Bag(map(sum, itertools.product(*dice)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ali: Here's a version of `sicherman` that takes triples:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{((1, 2, 2, 3, 3, 4), (1, 2, 3, 4, 5, 6), (1, 3, 4, 5, 6, 8))}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def sicherman3(N=6):\n",
    "    \"\"\"The set of triples of 6-sided dice that have the same\n",
    "    distribution of sums as a regular triple of dice.\"\"\"\n",
    "    reg_trip = 3 * (regular_die(N),)\n",
    "    reg_sums = sums(reg_trip)\n",
    "    return {trip for trip in triples(all_dice(N))\n",
    "            if trip != reg_trip\n",
    "            and sums(trip) == reg_sums}\n",
    "\n",
    "sicherman3()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Bo: Of course! I should have known. This is just a pair of Sicherman dice, with a regular die thrown in. But I didn't know whether there would be any other solutions; I see there aren't.** \n",
    "\n",
    "Ali: Maybe ... but  are we being too strict here? We defined `all_dice` to eliminate any die that wouldn't work as one of a Sicherman pair; but maybe we eliminated some that could work as a triple? Like, we decided that the biggest feasible number on a side was 8, but now that sums go all the way up to 18, not 12, maybe we should allow more? \n",
    "\n",
    "**Bo: Good point. But let's leave that question for the reader too (hi, reader!)**."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
